package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SharedPreferencesActivity extends AppCompatActivity implements View.OnClickListener {
    Button btn1;
    Button btn2;
    SharedPreferences sp;
    EditText et;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shared_preferences);

        btn1 = findViewById(R.id.btn1);
        btn2 = findViewById(R.id.btn2);
        et = findViewById(R.id.et);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        sp = getSharedPreferences("config",MODE_PRIVATE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn1:
                String text = et.getText().toString();
                saveDate(text);
                break;
            case R.id.btn2:
                String content = sp.getString("content","");
                Toast.makeText(this,content,Toast.LENGTH_SHORT).show();
                break;
        }
    }
    private void saveDate(String string){
        SharedPreferences.Editor editor= sp.edit();
        editor.putString("content",string);
        editor.commit();
    }
}